home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1996 April / Software of the Month Club 1996 April.iso / pc / os2 / psutils / src / psmerge.pl < prev    next >
Text File  |  1996-02-21  |  2KB  |  86 lines

  1. @PERL@
  2. # psmerge: merge PostScript files produced by same application and setup
  3. # usage: psmerge [-oout.ps] [-thorough] file1.ps file2.ps ...
  4. #
  5. # Copyright (C) Angus J. C. Duggan 1991-1995
  6. # See file LICENSE for details.
  7.  
  8. $prog = ($0 =~ s=.*/==);
  9.  
  10. while ($ARGV[0] =~ /^-/) {
  11.    $_ = shift;
  12.    if (/^-o(.+)/) {
  13.       if (!close(STDOUT) || !open(STDOUT, ">$1")) {
  14.      print STDERR "$prog: can't open $1 for output\n";
  15.      exit 1;
  16.       }
  17.    } elsif (/^-t(horough)?$/) {
  18.       $thorough = 1;
  19.    } else {
  20.       print STDERR "Usage: $prog [-oout] [-thorough] file...\n";
  21.       exit 1;
  22.    }
  23. }
  24.  
  25. $page = 0;
  26. $first = 1;
  27. $nesting = 0;
  28.  
  29. @header = ();
  30. $header = 1;
  31.  
  32. @trailer = ();
  33. $trailer = 0;
  34.  
  35. @pages = ();
  36. @body = ();
  37.  
  38. @resources = ();
  39. $inresource = 0;
  40.  
  41. while (<>) {
  42.    if (/^%%BeginFont:/ || /^%%BeginResource:/ || /^%%BeginProcSet:/) {
  43.       $inresource = 1;
  44.       push(@resources, $_);
  45.    } elsif ($inresource) {
  46.       push(@resources, $_);
  47.       $inresource = 0 if /^%%EndFont/ || /^%%EndResource/ || /^%%EndProcSet/;
  48.    } elsif (/^%%Page:/ && $nesting == 0) {
  49.       $header = $trailer = 0;
  50.       push(@pages, join("", @body)) if @body;
  51.       $page++;
  52.       @body = ("%%Page: ($page) $page\n");
  53.    } elsif (/^%%Trailer/ && $nesting == 0) {
  54.       push(@trailer, $_);
  55.       push(@pages, join("", @body)) if @body;
  56.       @body = ();
  57.       $trailer = 1;
  58.       $header = 0;
  59.    } elsif ($header) {
  60.       push(@trailer, $_);
  61.       push(@pages, join("", @body)) if @body;
  62.       @body = ();
  63.       $trailer = 1;
  64.       $header = 0;
  65.    } elsif ($trailer) {
  66.       if (/^%!/ || /%%EOF/) {
  67.      $trailer = $first = 0;
  68.       } elsif ($first) {
  69.      push(@trailer, $_);
  70.       }
  71.    } elsif (/^%%BeginDocument/ || /^%%BeginBinary/ || /^%%BeginFile/) {
  72.       push(@body, $_);
  73.       $nesting++;
  74.    } elsif (/^%%EndDocument/ || /^%%EndBinary/ || /^%%EndFile/) {
  75.       push(@body, $_);
  76.       $nesting--;
  77.    } else {
  78.       print $_ if $print;
  79.    }
  80. }
  81.  
  82. print @trailer;
  83.  
  84. exit 0;
  85. @END@
  86.